#include "communication.h"
#include <XBDrive/SLB/strproc.h>
#include <XBDrive/SLB/ringbuffer.h>

#define QUEUELENGTH 32
#define MAXLINELENGTH 64

RingBuffer serial_RX_buffer;

// Receive Task
TaskHandle_t serialReceiveTaskHandle = NULL;
// Parse Task
TaskHandle_t serialParseTaskHandle = NULL;
// queue handle
QueueHandle_t serialQueue;

// 串口数据接收
void serialReceive(void *pvParameters)
{
    char line[MAXLINELENGTH];
    int index = 0;
    serial_RX_buffer.init();
    while (1)
    {
        if (DEBUG_port->available())
        {
            char data_rece = DEBUG_port->read();
            line[index++] = data_rece;
            if (index == 64)
            {
                DEBUG_port->println("串口数据行溢出");
                index = 0;
            }
            else if (data_rece == '\n')
            {
                // // 删尾部字符
                // if (line[--index] == '\n')
                //     line[index] = '\0';
                // if (line[--index] == '\r')
                //     line[index] = '\0';
                // // 向队列中发送数据
                // BaseType_t res = xQueueSend(serialQueue, &line, 0);
                // if (res != pdPASS)
                // {
                //     DEBUG_port->println("串口数据接受失败");
                // }
                // // 索引置0
                // index = 0;
                line[index++] = data_rece;
                line[index] = '\0';
                serial_RX_buffer.enqueueArr(line, index);
                DEBUG_port->println(line);
                index = 0;
            }
        }
    }
}

// 串口数据解析
void SerialParse()
{
    char line[MAXLINELENGTH];
    char key[MAXLINELENGTH];
    char value[MAXLINELENGTH];
    char out[MAXLINELENGTH];
    while (1)
    {
        // // 从队列中读取数据
        // BaseType_t res = xQueueReceive(serialQueue, &line, 0);
        // if (res == pdPASS)
        // {
        //     DEBUG_port->printf(">>%s\r\n", line);
        //     if (strstr(line, "$") != NULL && strstr(line, "=") != NULL)
        //     {
        //         strgsr(line, line, '$');
        //         strgsl(key, line, '=');
        //         strgsr(value, line, '=');
        //         DEBUG_port->printf("key: %s, value: %s\r\n", key, value);
        //         do_command_or_setting(key, value, out);
        //         DEBUG_port->printf(">>%s\r\n", out);
        //     }
        //     else if (strstr(line, "$") != NULL && strstr(line, "=") == NULL)
        //     {
        //         strgsr(line, line, '$');
        //         strgsl(key, line, '=');
        //         DEBUG_port->printf("key: %s\r\n", key);
        //         do_command_or_setting(key, NULL, out);
        //         DEBUG_port->printf(">>%s\r\n", out);
        //     }

        //     memset(line, 0, sizeof(line));
        // }

        if (serial_RX_buffer.availableLine())
        {
            serial_RX_buffer.getLine(line);
            int len = strlen(line);
            if (line[len - 1] = '\n')
                line[len - 1] = '\0';
            if (line[len - 2] = '\r')
                line[len - 2] = '\0';
            //DEBUG_port->printf(">>%s\r\n", line);
            if (strstr(line, "$") != NULL && strstr(line, "=") != NULL)
            {
                strgsr(line, line, '$');
                strgsl(key, line, '=');
                strgsr(value, line, '=');
                //DEBUG_port->printf("key: %s, value: %s\r\n", key, value);
                do_command_or_setting(key, value, out);
                DEBUG_port->printf(">>%s\r\n", out);
            }
            else if (strstr(line, "$") != NULL && strstr(line, "=") == NULL)
            {
                strgsr(line, line, '$');
                strgsl(key, line, '=');
                //DEBUG_port->printf("key: %s\r\n", key);
                do_command_or_setting(key, NULL, out);
                DEBUG_port->printf(">>%s\r\n", out);
            }

            memset(line, 0, sizeof(line));
        }
    }
}

// 串口数据解析
void SerialParse(void *pvParameters)
{
    char line[MAXLINELENGTH];
    char key[MAXLINELENGTH];
    char value[MAXLINELENGTH];
    char out[MAXLINELENGTH];
    while (1)
    {
        // // 从队列中读取数据
        // BaseType_t res = xQueueReceive(serialQueue, &line, 0);
        // if (res == pdPASS)
        // {
        //     DEBUG_port->printf(">>%s\r\n", line);
        //     if (strstr(line, "$") != NULL && strstr(line, "=") != NULL)
        //     {
        //         strgsr(line, line, '$');
        //         strgsl(key, line, '=');
        //         strgsr(value, line, '=');
        //         DEBUG_port->printf("key: %s, value: %s\r\n", key, value);
        //         portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
        //         //portENTER_CRITICAL(&myMutex);
        //         do_command_or_setting(key, value, out);
        //         //portEXIT_CRITICAL(&myMutex);

        //         DEBUG_port->printf(">>%s\r\n", out);
        //     }
        //     else if (strstr(line, "$") != NULL && strstr(line, "=") == NULL)
        //     {
        //         strgsr(line, line, '$');
        //         strgsl(key, line, '=');
        //         DEBUG_port->printf("key: %s\r\n", key);
        //         portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
        //         //portENTER_CRITICAL(&myMutex);
        //         do_command_or_setting(key, NULL, out);
        //         //portEXIT_CRITICAL(&myMutex);
        //         DEBUG_port->printf(">>%s\r\n", out);
        //     }

        //     memset(line, 0, sizeof(line));
        // }

        if (serial_RX_buffer.availableLine())
        {
            serial_RX_buffer.getLine(line);
            int len = strlen(line);
            if (line[len - 1] = '\n')
                line[len - 1] = '\0';
            if (line[len - 2] = '\r')
                line[len - 2] = '\0';
            DEBUG_port->printf(">>%s\r\n", line);
            if (strstr(line, "$") != NULL && strstr(line, "=") != NULL)
            {
                strgsr(line, line, '$');
                strgsl(key, line, '=');
                strgsr(value, line, '=');
                DEBUG_port->printf("key: %s, value: %s\r\n", key, value);
                do_command_or_setting(key, value, out);
                DEBUG_port->printf(">>%s\r\n", out);
            }
            else if (strstr(line, "$") != NULL && strstr(line, "=") == NULL)
            {
                strgsr(line, line, '$');
                strgsl(key, line, '=');
                DEBUG_port->printf("key: %s\r\n", key);
                do_command_or_setting(key, NULL, out);
                DEBUG_port->printf(">>%s\r\n", out);
            }

            memset(line, 0, sizeof(line));
        }
    }
}

void SerialCheckTaskSetup()
{
    // 串口接收队列
    serialQueue = xQueueCreate(QUEUELENGTH, MAXLINELENGTH); //创建一个队列，用的是整数型
    // 创建接收任务
    xTaskCreatePinnedToCore(serialReceive,       // task
                            "serialReceiveTask", // name for task
                            4096,                // size of task stack
                            NULL,                // parameters
                            1,                   // priority
                            &serialReceiveTaskHandle,
                            CONFIG_ARDUINO_RUNNING_CORE);
    // // 创建解析任务
    // xTaskCreatePinnedToCore(SerialParse,       // task
    //                         "SerialParseTask", // name for task
    //                         4096,              // size of task stack
    //                         NULL,              // parameters
    //                         1,                 // priority
    //                         &serialParseTaskHandle,
    //                         CONFIG_ARDUINO_RUNNING_CORE);
}
